<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019-02-21
 * Time: 9:15
 */

namespace app\common\model;


use think\Db;
use think\Model;

class Drawingm extends Model
{

    public static function index($map = [], $page)
    {
        return Db::name('drawing')
            ->join('user', 'd_uid = u_id', 'left')
            ->where($map)
            ->field('d_id, d_uid, u_email, d_type, d_money_rmb, d_money_dollar, d_status, d_remark, from_unixtime(d_create_time) d_create_time, from_unixtime(d_update_time) d_update_time')
            ->order('d_status asc')
            ->order('d_id DESC')
            ->paginate(15, false, ['page' => $page]);
    }

    /**
     * 提现详情
     */
    public static function detail($id)
    {
        return Db::name('drawing')
            ->join('user', 'd_uid = u_id', 'left')
            ->where('d_id', $id)
            ->field('d_id, d_uid, u_email, d_type, d_money_rmb, d_money_dollar, d_status, d_remark,
            from_unixtime(d_create_time) d_create_time, from_unixtime(d_update_time) d_update_time,
            u_alipay, u_bank_card_code, u_bank_type, u_name_bank, u_bank_phone')
            ->find();
    }

    /**
     * 修改提现的状态
     */
    public static function updateStatus($data)
    {
        if ($data['d_status'] == 2) {//确认打款
               return Db::name('drawing')->where('d_id', $data['d_id'])->where('d_status', 1)
                    ->update([
                        'd_status'  => 2,
                        'd_update_time' => time()
                    ]);
        } elseif ($data['d_status'] == 3) {//拒绝打款
            Db::startTrans();
            try{
                 Db::name('drawing')->where('d_id', $data['d_id'])->where('d_status', 1)
                    ->update([
                        'd_status'  => 3,
                        'd_update_time' => time()
                    ]);
                Db::name('user')->where('u_id', $data['d_uid'])->setInc('u_dollar_balance', $data['d_money_dollar']);
                Db::commit();
                return true;
            } catch(\Exception $e) {
                Db::rollback();
                return false;
            }
        }else {
            return false;
        }

    }
}